<?php
/**
 * 修改店铺的
 * Author: lyun
 * Date: 2018/4/8
 * Created by Panxsoft.
 */


namespace App\Http\Controllers\App;

use App\Http\Controllers\ApiController;
use App\Http\Requests\App\StorePatchInfoRequest;
use App\Models\Store\Store;
use App\Services\Utils\Guard;

class StoreController extends ApiController
{
    const ROUTES = [
        'show-info' => 'app.store.show-info',
        'store-info' => 'app.store.store.info',
    ];

    /**
     * 显示店铺信息
     * @test
     *
     * @return \Illuminate\Http\Response
     */
    public function showInfo()
    {
        $store = Guard::deviceAccount()->store;
        $store->staffs = $store->storeStaffs()
            ->filterWechatBoundAndSubscribed()
            ->get();

        return $this->response(compact('store'));
    }

    /**
     * Update the specified resource in storage.
     * @test
     *
     * @param  StorePatchInfoRequest $request
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function patchInfo(StorePatchInfoRequest $request)
    {
        /** @var Store $store */
        $store = Guard::deviceAccount()->store;
            $this->checkItemExist($store, trans('app.store_error'));

        $store_data = $this->assemblePatchInfoData($request);

        $updated = $store->update($store_data);

        return $this->response(compact('updated', 'store'));
    }

    /**
     * @param StorePatchInfoRequest $request
     * @return array
     */
    private function assemblePatchInfoData(StorePatchInfoRequest $request)
    {
        return [
            'name'    => $request->name,
            'address' => $request->address,
            'phone'   => $request->phone,
        ];
    }
}
